package Sequence;

import java.util.ArrayList;
import java.util.List;

public class Test0257 {
}

class Solution0257_1 {
    List<String> ans = new ArrayList<>();

    public List<String> binaryTreePaths(TreeNode root) {
        if (root == null) return ans;
        StringBuilder stringBuilder =new StringBuilder();
        stringBuilder.append(root.val);
        if (root.right==null && root.left==null){
            ans.add(stringBuilder.toString());
        }else {
            if (root.left!=null){
                traverse(root.left,stringBuilder,stringBuilder.length());
            }

            if (root.right!=null){
                traverse(root.right,stringBuilder,stringBuilder.length());
            }
        }
        return ans;
    }

    private void traverse(TreeNode root,StringBuilder stringBuilder,int length) {
        stringBuilder.append("->").append(root.val);

        if (root.right==null && root.left==null){
            ans.add(stringBuilder.toString());
        }else {
            if (root.left!=null){
                traverse(root.left,stringBuilder,stringBuilder.length());
            }

            if (root.right!=null){
                traverse(root.right,stringBuilder,stringBuilder.length());
            }
        }

        stringBuilder.delete(length,stringBuilder.length());
    }
}
